var reviewJurisdictionData = [];
var updateNowData = [];
var deleteFlag = undefined;
var index = undefined;
var deleteFlag = '';

$(document).ready(function() {
	showLoading();
	setBaseUrl();
	$('title', parent.document).html($('title').text());
	createTableHead("reviewJurisdictionThead", reviewJurisdictionTable);
	search();
});
function getUserName(data) {
	$("#userID").empty();
	$("#userID").append("<option value=''>请选择</option>");
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#userID").append("<option value='" + data[i].userID + "'>" + data[i].userName + "</option>");
	}
	$ajaxList('subBranchCompany', getSubBranchCompanyName);
}
function getSubBranchCompanyName(data) {
	$("#subBranchCompanyID").empty();
	$("#subBranchCompanyID").attr("onchange","changeCarTeam()");
	$("#subBranchCompanyID").append("<option value=''>请选择</option>");
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#subBranchCompanyID").append("<option value='" + data[i].subBranchCompanyID + "'>" + data[i].subBranchCompanyName + "</option>");
	}
	if (deleteFlag == "update") {
		$("#userID").val(updateNowData.userID);
		$("#subBranchCompanyID").val(updateNowData.subBranchCompanyID);
		changeCarTeam();
	}
}
function changeCarTeam(){
	var subBranchCompanyID = $("#subBranchCompanyID").val();
	if (subBranchCompanyID) {
		$("#teamID").empty();
		$ajaxList('carTeam', getCarTeam, {"subBranchCompanyID": subBranchCompanyID});
	} else {
		$("#teamID").empty();
	}
}
function getCarTeam(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#teamID").append('<label style="margin-right:10px;margin-top:5px;"><input name="teamID" type="checkbox" style="" value="'+ data[i].teamID +'" />' + data[i].teamName + '</label>');
	}
	if (deleteFlag == "update") {
		var checks = document.getElementsByName("teamID");
		var teamID= updateNowData.teamID;
		var ID=teamID.split(",");
		for (var i=0;i<ID.length;i++) {
			for (var j=0; j<data.length; j++) {
				 if (ID[i] == data[j].teamID) {
					 checks[j].checked = true;
				  }
			}
		}
	}
}
function search(){
	$ajax('searchReviewJurisdiction', 'reviewJurisdictionSearch', reviewJurisdictionSearch);
	setTimeout(hideLoading, 500);
}
function reviewJurisdictionSearch(data){
	reviewJurisdictionData = data && data.userInfoList || [];
	createTableBody("reviewJurisdictionTbody", reviewJurisdictionTable, reviewJurisdictionData, false, true);
}
function save(index){
	createComBody("detailOneBody", reviewJurisdictionTable, reviewJurisdictionData[index], 1);
	$ajaxList('userName', getUserName);
	deleteFlag = "save";
	addTitle("reviewJurisdictionSave");
	$("#save").modal('show');
}
function saveInfo(){
	var teamID = '';
	var checks = document.getElementsByName("teamID");
	for(i=0;i<checks.length;i++){
        if(checks[i].checked) {
        	teamID += document.getElementsByName("teamID")[i].value + ",";
        }
	}
    $("#teamID").val(teamID.substring(0,teamID.length-1));
	if (deleteFlag == "save") {
		$ajaxSubmit("saveForm", "saveReviewJurisdiction", saveReviewJurisdiction);
	} else if (deleteFlag == "update") {
		$ajaxSubmit("saveForm", "updateReviewJurisdiction", saveReviewJurisdiction);
	}
}
function saveReviewJurisdiction(data){
	if(deleteFlag == "save"){
		var result = data && data.result;
		if (result == "Success") {
			setTimeout(hideLoading, 500);
			showMessge(messageContent["Insert.Success"]);
			$('#save').modal('hide');
			formReset("saveForm");
		} else {
			showMessge(messageContent["Insert.Error"]);
		}
	} else if (deleteFlag == "update") {
		var result = data && data.result;
		if (result == "Success") {
			setTimeout(hideLoading, 500);
			showMessge(messageContent["Update.Success"]);
			$('#save').modal('hide');
			formReset("saveForm");
		} else {
			showMessge(messageContent["Update.Error"]);
		}
	}
	search();
}

function chkBefore(num, flag){
	deleteFlag = flag;
	updateNowData = reviewJurisdictionData[num];
	if (deleteFlag == "update") {
		createComBody("detailOneBody", reviewJurisdictionTable, reviewJurisdictionData[index], 1);
		$ajaxList('userName', getUserName);
		addTitle("reviewJurisdictionUpdate");
		$("#save").modal('show');
	} else if (deleteFlag == "delete"){
		showMessge(messageContent["Delete.Confirm"], "model2");
	}
}
function getUrl(){
	return 'deleteReviewJurisdiction';
}
function getData(){
	return {UserID: updateNowData.userID};
}
function sure(data){
	var result = data && data.result;
    if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
    search();
}